package com.luck.emp.ex.logic;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.common.SpOrderNoFour;
import com.luck.emp.common.SpWhOrderD;
import com.luck.emp.common.SpWhOrderM;
import com.luck.emp.entity.EmpTProdBase;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.entity.EmpTWhExD;
import com.luck.emp.entity.EmpTWhExM;
import com.luck.emp.ex.qry.ExQry;
import com.luck.org.entity.VDepartment;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;

/**
 * 说明
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class ExLogic extends BaseLogic implements IExLogic{
	private BaseJdbcDao jdbcDao;
	
	/**
	 * 查找分页信息
	 */
	public <T> T findExPage(ExQry qry, Class<T> cls){
		Criteria criteria = baseDao.createCriteria(EmpTWhExM.class);		
		CriteriaUtil.addEq(criteria, "status", qry.getStatus());	
		CriteriaUtil.addFullLike(criteria, "exNo", qry.getExNo());	
		CriteriaUtil.addFullLike(criteria, "tel", qry.getTel());
		Order order=Order.desc("exNo");
		criteria.addOrder(order);
		
		criteria.createAlias("dept", "d");
		filterData(criteria, qry.getUserInfo(), "d", null);
		if (cls.getName().equals("java.util.List")) {
			return (T) criteria.list();
		} else {
			return (T) baseDao.pagedQuery(criteria, qry.getPageNo(),qry.getPageSize());
		}
		//return baseDao.pagedQuery(criteria,qry.getPageNo(),qry.getPageSize());
	}	
	
	/**
	 * 保存
	 **/
	public String saveEx(UserBean userBean, EmpTWhExM entity, String[] productCodes, String[]  qtys) throws Exception{
		StringBuffer bf = new StringBuffer();
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());
		EmpTWarehouse fromWh = super.get(EmpTWarehouse.class, entity.getFromWhCode());
		String billType = "13";//调拔单
		String ioType = "44";//调拨出库
		if(StringUtils.isBlank(entity.getExNo())){//新增
			SpOrderNoFour sp = new SpOrderNoFour(this.jdbcDao.getJdbcTemplate());
			String dbCode = sp.execute(userBean.getDept().getDeptCode(), "DBCK");//调拨出库
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userBean.getUser());
			entity.setDeptId(userBean.getDeptId());
			entity.setFromDeptId(fromWh.getDeptId());
			entity.setBillType(billType);//调拔单
			entity.setInoutType(ioType);//调拨出库
			entity.setExNo(dbCode);
			entity.setStatus("10");//新建
		}
		save(entity);

		//增加明细
		for(int i=0; i < productCodes.length; i ++){
			EmpTWhExD exd = new EmpTWhExD();
			Long qty = Long.parseLong(qtys[i]);
			EmpTProdBase product = super.get(EmpTProdBase.class, productCodes[i]);
			if(fromWh.getWhMode().equals("1"))//简单仓
				exd.setConfirmOut(qty);
			exd.setExNo(entity.getExNo());
			exd.setProductCode(productCodes[i]);
			exd.setQty(qty);
			exd.setPrice(product.getSale().getPrice());
			exd.setPurPrice(product.getSale().getPurPrice());
			exd.setStdPrice(product.getSale().getStdPrice());
			exd.setStockPrice(product.getSale().getStockPrice());
			save(exd);
		}
		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isNotBlank(bf.toString())) {
			throw new Exception(bf.toString());
		} 
		super.save(entity);
		return bf.toString();
	}
	public String saveConfirmEx(UserBean userBean, String exCode) throws Exception{
		StringBuffer bf = new StringBuffer();
		EmpTWhExM entity = super.get(EmpTWhExM.class, exCode);
		String billType = "13";//调拔单
		String ioType = "44";//调拨出库
		if(!entity.getStatus().equals("10"))
			bf.append("<br>只能新建状态才能操作");
		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isNotBlank(bf.toString())) {
			throw new Exception(bf.toString());
		} 
		List<EmpTWhExD> infos = findExdByExCode(exCode);
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());
		entity.setAuditDate(DateUtil.getDate());
		entity.setAuditor(userBean.getUser());
		if(entity.getFromWh().getWhMode().equals("1"))//简单仓
			entity.setStatus("18");//完成出库
		else
			entity.setStatus("14");//出库中
		save(entity);

		//处理出入库
		SpWhOrderM sp = new SpWhOrderM(this.jdbcDao.getJdbcTemplate());
		String orderCode = sp.execute(billType, 20L, entity.getExNo(), entity.getFromWhCode(), null, 
				entity.getContact(), entity.getTel(), "", entity.getNote(), userBean.getUserId(), ioType, null, null, entity.getSalemanId());

		SpWhOrderD spd = new SpWhOrderD(this.jdbcDao.getJdbcTemplate());
		for(EmpTWhExD info:infos){
			// 插入订单明细表
			spd.execute(orderCode, info.getProductCode(), info.getQty(), 0, 0, userBean.getUserId(), null);
		}
		return bf.toString();
	}
	public List<EmpTWhExD> findExdByExCode(String exCode){
		return super.baseDao.find(" FROM EmpTWhExD WHERE exNo=?", exCode);
	}
	public List<VDepartment> findExDept(UserBean userBean){
		List ls = new ArrayList();
		if(userBean.getDept().getDeptKind().equals("1")){//为机构
			ls = super.baseDao.find("FROM VDepartment WHERE treeNo like ? and id<>?",new Object[]{userBean.getDept().getTreeNo()+"%",userBean.getDeptId()});
		}else{
			ls = super.baseDao.find("FROM VDepartment WHERE treeNo like ? and id<>?",new Object[]{userBean.getDept().getParentDept().getTreeNo()+"%",userBean.getDeptId()});
		}
		
		return ls;
	}
	/**
	 * 删除
	 */
	public String deleteEx(UserBean userBean, EmpTWhExM entity) throws Exception{
		StringBuffer bf = new StringBuffer();
		if(!entity.getStatus().equals("10"))
			bf.append("<br>只能新建状态才能操作");

		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isNotBlank(bf.toString())) {
			throw new Exception(bf.toString());
		} 
		super.delete(entity);
		super.baseDao.getHibernateTemplate().deleteAll(findExdByExCode(entity.getExNo()));
		return bf.toString();
	}
	
	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}